
#include "Weight.h"
#include "includes.h"
#include <string.h> 
#include <stdio.h>  
#include <stdbool.h>
#include <IOdevice.h>
#include "xrj_protocol.h"
#include "usart1.h"
#include "usart2.h"
#include "usart3.h"
#include "usart5.h"

#if 1
UINT8 Weigth_Get[5] = {0x00, 0x05, 0x02, 0x05 , 0x0C};	
UINT8 Weigth_SetZero[5] = {0x00, 0x63, 0x06, 0x01, 0x6A};

UINT8 Weight_Read_Buf[32];
INT32 Cnt_test = 0;

UINT8 LCR_Check(UINT8 * buf, INT32 len)
{
UINT8 ret = 0;
INT32 i = 0;
	for (i = 0; i< len ; i++)
	{
		ret += buf[i];
	}
	return ret;
}
//----0.0001
INT32 Division_Tab[15] = {1,2,5,
			10,20,50,
			100,200,500,
			1000,2000,5000,
			10000,20000,50000,
			};

#define	D_SetCmd_CLEAR		0xFF		

INT64 ToWeight(UINT8 *buf,INT32 len)
{
INT32 div;
INT32 weight;
INT64 ret;	
	if ((buf[0]&0x0f) == 0x0f)
	{
		return 0;
	}	
	
	div = Division_Tab[buf[0]&0x0f];
	
	weight = buf[1];
	weight <<=8;
	weight += buf[2]; 
	weight <<=8;	
	weight += buf[3]; 
	ret = weight * div/10;
	if (buf[0]&0x80)
	{
		ret *= (-1);
	}	
	
	
	return ret;
}

INT32 Response_Decode(UINT8 * buf, INT32 len, INT64 *Weight)
{
	if ((buf[1] == 0x06) && (buf[2] == 0x02))
	{
		*Weight = ToWeight(&buf[4],4);
		return 0;
	}
	if ((buf[1] == 0x64) && (buf[2] == 0x06))
	{
		printf("Weight  Clear \n");
		return 1;
	}	
	
	return 1;
}	

#if 0
void Weight_Proc(void)
{


INT64 Weight_Report = 0;	
INT32 len;
INT32 ret;
INT64 Weight_Set1 = 0;	
INT64 Weight_Set2 = 0;		

	
	Cnt_test++;
	if (Cnt_test > 100)
	{	
		Cnt_test = 0;	
		USART5_Write((UINT8*)Weigth_Get,sizeof(Weigth_Get));
		USART3_DMA_WRITE((UINT8*)Weigth_Get,sizeof(Weigth_Get));
	}
	else if (Cnt_test == 50)
	{
		XRJ_mutex_lock(&Device_Value.Lock);
		Weight_Set1 = Device_Value.WEIGHING_1_Setting;
		Weight_Set2 = Device_Value.WEIGHING_2_Setting;
		Device_Value.WEIGHING_1_Setting = 0;
		Device_Value.WEIGHING_2_Setting = 0;
		XRJ_mutex_unlock(&Device_Value.Lock);
		
		if (Weight_Set1 == D_SetCmd_CLEAR)
		{
			USART5_Write((UINT8*)Weigth_SetZero,sizeof(Weigth_SetZero));
		}
		
		if (Weight_Set2 == D_SetCmd_CLEAR)
		{
			USART3_DMA_WRITE((UINT8*)Weigth_SetZero,sizeof(Weigth_SetZero));
		}		
	}	
	
	
	
	
	memset(Weight_Read_Buf,0, sizeof(Weight_Read_Buf));
	len = USART5_Read_NoBlock(Weight_Read_Buf , sizeof(Weight_Read_Buf));
	if (len > 0)
	{	
		if ((len < 20) &&
			(LCR_Check(Weight_Read_Buf,(len-1)) == Weight_Read_Buf[len - 1]))
		{	
			ret = Response_Decode(Weight_Read_Buf,len,&Weight_Report);
			if (ret == 0)
			{	
				XRJ_mutex_lock(&Device_Value.Lock);
				Device_Value.WEIGHING_1_Report = Weight_Report;
				XRJ_mutex_unlock(&Device_Value.Lock);
				printf("Weight1:[%lld]\n", Weight_Report);
			}			
		}
	}	
	
	memset(Weight_Read_Buf,0, sizeof(Weight_Read_Buf));
	len = USART3_Read_NoBlock(Weight_Read_Buf , sizeof(Weight_Read_Buf));
	if (len > 0)
	{	
		if ((len < 20) &&
			(LCR_Check(Weight_Read_Buf,(len-1)) == Weight_Read_Buf[len - 1]))
		{	
			ret = Response_Decode(Weight_Read_Buf,len,&Weight_Report);
			if (ret == 0)
			{	
				XRJ_mutex_lock(&Device_Value.Lock);
				Device_Value.WEIGHING_2_Report = Weight_Report;
				XRJ_mutex_unlock(&Device_Value.Lock);
				printf("Weight2:[%lld]\n", Weight_Report);
			}	
			
		}
	}	
}	
#endif

void Weight_Proc_1(void)
{


INT64 Weight_Report = 0;	
INT32 len;
INT32 ret;
INT64 Weight_Set1 = 0;	
INT64 Weight_Set2 = 0;		

	
	Cnt_test++;
	if (Cnt_test > 100)
	{	
		Cnt_test = 0;	
		USART5_Write((UINT8*)Weigth_Get,sizeof(Weigth_Get));
	}
	else if (Cnt_test == 50)
	{
		XRJ_mutex_lock(&Device_Value.Lock);
		Weight_Set1 = Device_Value.WEIGHING_1_Setting;
		Device_Value.WEIGHING_1_Setting = 0;
		XRJ_mutex_unlock(&Device_Value.Lock);
		
		if (Weight_Set1 == D_SetCmd_CLEAR)
		{
			USART5_Write((UINT8*)Weigth_SetZero,sizeof(Weigth_SetZero));
		}
					
	}	
	
	memset(Weight_Read_Buf,0, sizeof(Weight_Read_Buf));
	len = USART5_Read_NoBlock(Weight_Read_Buf , sizeof(Weight_Read_Buf));
	if (len > 0)
	{	
		if ((len < 20) &&
			(LCR_Check(Weight_Read_Buf,(len-1)) == Weight_Read_Buf[len - 1]))
		{	
			ret = Response_Decode(Weight_Read_Buf,len,&Weight_Report);
			if (ret == 0)
			{	
				XRJ_mutex_lock(&Device_Value.Lock);
				Device_Value.WEIGHING_1_Report = Weight_Report;
				XRJ_mutex_unlock(&Device_Value.Lock);
				printf("Weight1:[%lld]\n", Weight_Report);
			}			
		}
	}	
	
}

void Weight_Proc(void)
{
INT64 Weight_Report = 0;	
INT32 len;
INT32 ret;
INT64 Weight_Set1 = 0;	
INT64 Weight_Set2 = 0;		

	
	XRJ_mutex_lock(&Device_Value.Lock);
	Weight_Set2 = Device_Value.WEIGHING_2_Setting;
	Device_Value.WEIGHING_2_Setting = 0;
	XRJ_mutex_unlock(&Device_Value.Lock);
	if (Weight_Set2 == D_SetCmd_CLEAR)
	{
		XRJ_mutex_lock(&Device_Value.Lock);
		Device_Value.WEIGHING_2_Report = 100000;
		XRJ_mutex_unlock(&Device_Value.Lock);
	}
	Weight_Proc_1();
}	




#else
UINT32	Cnt_Weight_Read = 0;
UINT8 Weigth_Get[5] = {0x00, 0x05, 0x02, 0x05 , 0x0C};	
UINT8 Weight_Read_Buf[32];
#if 0
const UINT8 Weigth_SetConfig[18] = {0x02, 0x53, 0x45 ,0x54, 0x40, 0x30 ,0x33 ,0x30 ,0x2E ,0x30, 0x30 ,0x30,0x75 ,0x30 ,0x32 ,0x32 , 0x86, 0x0D };
//const UINT8 Weigth_SetConfig[18] = {0x02 ,0x53 ,0x45 ,0x54 ,0x40 ,0x30 ,0x30 ,0x34 ,0x30 ,0x30 ,0x30 ,0x30 ,0x6C ,0x31 ,0x32 ,0x32 ,0x81 ,0x0D};
const UINT8 Weigth_GetConfig[7] = {0x02 ,0x52 ,0x53 ,0x45 ,0x40 ,0xAA ,0x0D};
const UINT8 Weigth_SetZero_Nolimit[7] = {0x02,0x52,0x5A,0x52,0x7F,0xFD,0x0D};	
const UINT8 Weigth_SetZero[7] = {0x02,0x52,0x5A,0x52,0x40,0xBE,0x0D};
const UINT8 Weigth_Check[14] = {0x02 ,0x43 ,0x41 ,0x53 ,0x7F ,0x30 ,0x30 ,0x30 ,0x34 ,0x30 ,0x30 ,0x30 ,0xAA ,0x0D}; 
//const UINT8 Weigth_Check[14] = {0x02 ,0x43 ,0x41 ,0x53 ,0x7F ,0x30 ,0x30 ,0x30 ,0x32 ,0x30 ,0x30 ,0x30 ,0xA8 ,0x0D}; 
#endif

//---设置量程
const UINT8 Weigth_SetConfig[18] = {0x02 ,0x53 ,0x45 ,0x54 ,0x40 ,0x30 ,0x30 ,0x33 ,0x30 ,0x30 ,0x30 ,0x30 ,0x6C ,0x30 ,0x30 ,0x32 ,0xFD ,0x0D};
//---清零	
//const UINT8 Weigth_SetZero_Nolimit[7] = {0x02,0x52,0x5A,0x52,0x40,0xBE,0x0D};
const UINT8 Weigth_SetZero_Nolimit[7] = {0x02,0x52,0x5A,0x52,0x7F,0xFD,0x0D};	
//---获取量程和测量值
const UINT8 Weigth_GetConfig[7] = {0x02 ,0x52 ,0x53 ,0x45 ,0x40 ,0xAA ,0x0D};
//---校准	20kg
//const UINT8 Weigth_Check[14] = {0x02 ,0x43 ,0x41 ,0x53 ,0x7F ,0x30 ,0x30 ,0x30 ,0x32 ,0x30 ,0x30 ,0x30 ,0xA8 ,0x0D}; 
//---校准	2kg
const UINT8 Weigth_Check[14] = {0x02 ,0x43 ,0x41 ,0x53 ,0x7F ,0x30 ,0x30 ,0x30 ,0x30 ,0x32 ,0x30 ,0x30 ,0xA8 ,0x0D}; 


#define DEBUG_WEIGHT	1
enum{
	WeightID = 0,
	WeightCode = 1,
	WeightRegister = 2,
};

INT32 Weight_Check_Value(UINT8 *Buf, INT32 len, INT32 *pWeight)
{	
INT32 ret = -1;	
UINT8 check = 0;	
INT32 i = 0;	
INT32 weight;	
UINT32  TMP;	
	if (len != 9)
	{
		return ret;
	}
	else if (Buf[WeightCode] != 0x06)
	{
		return ret;
	}	
	else if (Buf[WeightRegister] != 0x02)
	{
		return ret;
	}	

	for (i = 0; i < (len-1); i++)
	{
		check += Buf[i];
	}
	if	(check != Buf[8])
	{
		return ret;
	}	

	TMP = Buf[5];
	weight = TMP<<16;
	TMP = Buf[6];
	weight += TMP<<8;
	weight +=Buf[7];

	if (Weight_Read_Buf[4] == 0x84)
	{
		weight *= (-2); 
	}
	if (Weight_Read_Buf[4] == 0x04)
	{
		weight *= (2);
	}	 
	*pWeight = weight;
	
	return 0;
}

INT32 Weight_Check_Value_0(UINT8 *Buf, INT32 len, INT32 *pWeight)	
{
INT32 i = 0;
INT32 ret = -1;	
INT32 weight = 0;	
	if ((Buf[0] == 0x0A)
		&& (Buf[1] == 0x0D)
		&& ((Buf[2] == '-')||(Buf[2] == '+')))
	{

		ret = 0;
		for (i=3; i<len; i++)
		{
			
			if ((Buf[i]<0x30) || (0x39 < Buf[i]))
			{
				ret = -1;
				break;
			}	
			weight = weight*10+ Buf[i]-0x30;
		}
		if (ret == 0)
		{
			if(Buf[2] == '-')
			{
				weight = (-weight);
			}	
			*pWeight = weight;
		}	
	}
	return ret;
}

INT32 Weight_Check_ACK(UINT8 *Buf, INT32 len)	
{
	
	//if (strstr((INT8 *)Buf,"40000"))
	if (strstr((INT8 *)Buf,"30000"))
	{
		return 0;
	}	
	else if ((Buf[0] == 0x02)&&(Buf[1] == 0x40))
	{
		return 0;
	}
	else if (strstr((INT8 *)Buf,"OK"))
	{
		return 0;
	}
	else if (strstr((INT8 *)Buf,"WT"))
	{
		return 1;
	}		
	else if(strstr((INT8 *)Buf,"ER"))
	{
		return -1;
	}	
	return -2;	
}



INT32 Weight_Set_Proc1 = 0;
INT32 Weight_Set_Proc2 = 0;

INT32 Weight_Set_Timeout1 = 0;
INT32 Weight_Set_Timeout2 = 0;


INT32 Weight_Check_Proc1 = 0;
INT32 Weight_Check_Proc2 = 0;

INT32 Weight_Check_Timeout1  = 0;
INT32 Weight_Check_Timeout2  = 0;


enum{
	D_WEIGHT_IDLE	 			= 0,	
	D_WEIGHT_GETCONFIG_SEND 	= 1,
	D_WEIGHT_GETCONFIG_ACK 		= 2,
	D_WEIGHT_DELAY_1			= 3,
	D_WEIGHT_SETCONFIG_Send 	= 4,
	D_WEIGHT_SETCONFIG_ACK	 	= 5,
	D_WEIGHT_DELAY_2			= 6,
	D_WEIGHT_ZERO_Send 	 		= 7,
	D_WEIGHT_ZERO_ACK 	 		= 8,
};
enum{
	D_WEIGHT_CHECK_IDLE = 0,
	D_WEIGHT_CHECK_SEND = 3,
	D_WEIGHT_CHECK_ACK  = 4,
};	

UINT32 ui_SetDelay1 = 0;
UINT32 ui_SetDelay2 = 0;

void Weight_Check(void)
{
	if (Weight_Check_Proc1 == D_WEIGHT_GETCONFIG_SEND)
	{
		USART5_Write((UINT8*)Weigth_GetConfig,sizeof(Weigth_GetConfig));
		Weight_Check_Proc1 = D_WEIGHT_GETCONFIG_ACK;
	}	
	
	if (Weight_Check_Proc1 == D_WEIGHT_CHECK_SEND) 
	{
		printf("Weigth_Check1\n");
		USART5_Write((UINT8*)Weigth_Check,sizeof(Weigth_Check));
		Weight_Check_Proc1 = D_WEIGHT_CHECK_ACK;
	}
	
	if (Weight_Check_Proc1 != D_WEIGHT_CHECK_IDLE)
	{	
		Weight_Check_Timeout1++;
		if (Weight_Check_Timeout1 > 500)
		{
			Weight_Check_Proc1 = D_WEIGHT_CHECK_IDLE;
		}	
	}
	
	
	
	if (Weight_Check_Proc2 == D_WEIGHT_GETCONFIG_SEND)
	{
		USART3_DMA_WRITE((UINT8*)Weigth_GetConfig,sizeof(Weigth_GetConfig));
		Weight_Check_Proc2 = D_WEIGHT_GETCONFIG_ACK;
	}	
	
	if (Weight_Check_Proc2 == D_WEIGHT_CHECK_SEND) 
	{
		USART3_DMA_WRITE((UINT8*)Weigth_Check,sizeof(Weigth_Check));
		Weight_Check_Proc2 = D_WEIGHT_CHECK_ACK;
	}	
	

	
	if (Weight_Check_Proc2 != D_WEIGHT_CHECK_IDLE)
	{	
		Weight_Check_Timeout2++;
		if (Weight_Check_Timeout2 > 500)
		{
			Weight_Check_Proc2 = D_WEIGHT_CHECK_IDLE;
		}	
	}	
	
}	

void Weight_Set(void)
{

	if (Weight_Set_Proc1 == D_WEIGHT_GETCONFIG_SEND)
	{
		USART5_Write((UINT8*)Weigth_GetConfig,sizeof(Weigth_GetConfig));
		Weight_Set_Proc1 = D_WEIGHT_GETCONFIG_ACK;
	}	
	if (Weight_Set_Proc1 == D_WEIGHT_SETCONFIG_Send) 
	{
		USART5_Write((UINT8*)Weigth_SetConfig,sizeof(Weigth_SetConfig));
		Weight_Set_Proc1 = D_WEIGHT_SETCONFIG_ACK;
	}	
	
	if (Weight_Set_Proc1 == D_WEIGHT_ZERO_Send) 
	{
		USART5_Write((UINT8*)Weigth_SetZero_Nolimit,sizeof(Weigth_SetZero_Nolimit));
		Weight_Set_Proc1 = D_WEIGHT_ZERO_ACK;
	}		
	
	
	
	if (Weight_Set_Proc2 == D_WEIGHT_GETCONFIG_SEND)
	{
		USART3_DMA_WRITE((UINT8*)Weigth_GetConfig,sizeof(Weigth_GetConfig));
		Weight_Set_Proc2 = D_WEIGHT_GETCONFIG_ACK;
	}	
	
	if (Weight_Set_Proc2 == D_WEIGHT_SETCONFIG_Send) 
	{
		USART3_DMA_WRITE((UINT8*)Weigth_SetConfig,sizeof(Weigth_SetConfig));
		Weight_Set_Proc2 = D_WEIGHT_SETCONFIG_ACK;
	}	
	
	if (Weight_Set_Proc2 == D_WEIGHT_ZERO_Send) 
	{
		USART3_DMA_WRITE((UINT8*)Weigth_SetZero_Nolimit,sizeof(Weigth_SetZero_Nolimit));
		Weight_Set_Proc2 = D_WEIGHT_ZERO_ACK;
	}	
	
	if (Weight_Set_Proc1 != D_WEIGHT_IDLE)
	{	
		Weight_Set_Timeout1++;
		if (Weight_Set_Timeout1 > 500)
		{
			Weight_Set_Proc1 = D_WEIGHT_IDLE;
		}	
	}
	
	if (Weight_Set_Proc2 != D_WEIGHT_IDLE)
	{	
		Weight_Set_Timeout2++;
		if (Weight_Set_Timeout2 > 500)
		{
			Weight_Set_Proc2 = D_WEIGHT_IDLE;
		}	
	}	
	
	
	if ((Weight_Set_Proc1 == D_WEIGHT_DELAY_1) || (Weight_Set_Proc1 == D_WEIGHT_DELAY_2))
	{	
		ui_SetDelay1++;
		if (ui_SetDelay1 > 10)
		{
			ui_SetDelay1 = 0;
			Weight_Set_Proc1++;
		}	
	}
	
	if ((Weight_Set_Proc2 == D_WEIGHT_DELAY_1) || (Weight_Set_Proc2 == D_WEIGHT_DELAY_2))
	{	
		ui_SetDelay2++;
		if (ui_SetDelay2 > 10)
		{
			ui_SetDelay2 = 0;
			Weight_Set_Proc2++;
		}	
	}	
	
	
}

void Weight_Proc(void)
{
INT32 len;	
INT32 weight;
INT64 Weight_Set1 = 0;	
INT64 Weight_Set2 = 0;	
INT32 ret= 0;	
INT32 i = 0;
	XRJ_mutex_lock(&Device_Value.Lock);
	Weight_Set1 = Device_Value.WEIGHING_1_Setting;
	Weight_Set2 = Device_Value.WEIGHING_2_Setting;
	Device_Value.WEIGHING_1_Setting = 0;
	Device_Value.WEIGHING_2_Setting = 0;
	XRJ_mutex_unlock(&Device_Value.Lock);
	 
	if (Weight_Set1 == 0xFF)
	{
		if (Weight_Set_Proc1 == D_WEIGHT_IDLE)
		{
			Weight_Set_Proc1 = D_WEIGHT_GETCONFIG_SEND;
			Weight_Set_Timeout1 = 0;	
			ui_SetDelay1 = 0;
		}	
	}	
	else if (Weight_Set1 == 0xF0)
	{
		if (Weight_Check_Proc1 == D_WEIGHT_CHECK_IDLE)
		{
			Weight_Check_Proc1 = D_WEIGHT_CHECK_SEND;
			Weight_Check_Timeout1 = 0;	
			ui_SetDelay1 = 0;
		}			
	}	
	
	if (Weight_Set2 == 0xFF)
	{
		if (Weight_Set_Proc2 == D_WEIGHT_IDLE)
		{
			Weight_Set_Proc2 = D_WEIGHT_GETCONFIG_SEND;
			Weight_Set_Timeout2 = 0;
			ui_SetDelay2 = 0;
		}	
	}
	else if (Weight_Set2 == 0xF0)
	{
		if (Weight_Check_Proc2 == D_WEIGHT_CHECK_IDLE)
		{
			Weight_Check_Proc2 = D_WEIGHT_CHECK_SEND;
			Weight_Check_Timeout2 = 0;	
			ui_SetDelay2 = 0;
		}			
	}	
	
	Weight_Set();
	Weight_Check();
	memset(Weight_Read_Buf,0, sizeof(Weight_Read_Buf));
	len = USART5_Read_NoBlock(Weight_Read_Buf , sizeof(Weight_Read_Buf));
	if (len > 0)
	{	
		//printf("UART3:%s\n",Weight_Read_Buf);
		if (Weight_Check_Value_0(Weight_Read_Buf,len,&weight) == 0)
		{
			//printf("%s\n",Weight_Read_Buf);
			//printf("WEIGHING_1: %d\n", weight);
			XRJ_mutex_lock(&Device_Value.Lock);
			Device_Value.WEIGHING_1_Report = weight*10;
			XRJ_mutex_unlock(&Device_Value.Lock);
		}	
		else 
		{	
		
			for (i = 0; i < len; i++)
				printf("%02x ",Weight_Read_Buf[i]);
			printf("\n");
			printf("%s\n",Weight_Read_Buf);
			
			ret = Weight_Check_ACK(Weight_Read_Buf,len);
     		 if ((ret == 0) && (Weight_Set_Proc1 != D_WEIGHT_IDLE))
			{	
				#if DEBUG_WEIGHT
				printf("WEIGHING_1 set[%d]\n",Weight_Set_Proc1);
				#endif	
				Weight_Set_Proc1++; 	
				if (Weight_Set_Proc1 > D_WEIGHT_ZERO_ACK)
				{
					Weight_Set_Proc1 = D_WEIGHT_IDLE;
				}	
			}
			else if ((ret == 1) && (Weight_Set_Proc1 != D_WEIGHT_IDLE))
			{
				#if DEBUG_WEIGHT
				printf("WEIGHING_1 set WT\n");
				#endif
			}	
			else if ((ret == -1) && (Weight_Set_Proc1 != D_WEIGHT_IDLE))
			{
				Weight_Set_Proc1 = D_WEIGHT_IDLE;
				printf("WEIGHING_1 Set: Er\n");
			}
			
			
			else if ((ret == 0) && (Weight_Check_Proc1 != D_WEIGHT_CHECK_IDLE))
			{
				printf("WEIGHING_1 CHECK[%d]\n",Weight_Check_Proc1);
				Weight_Check_Proc1++; 
				if (Weight_Check_Proc1 > D_WEIGHT_CHECK_ACK)
				{
					Weight_Check_Proc1 = D_WEIGHT_CHECK_IDLE;
				}
			}	
			else if  ((ret == 1) && (Weight_Check_Proc1 != D_WEIGHT_CHECK_IDLE))
			{
				printf("WEIGHING_2 CHECK WT\n");
			}	
			else if  ((ret == -1) && (Weight_Check_Proc1 != D_WEIGHT_CHECK_IDLE))
			{
				Weight_Check_Proc1 = D_WEIGHT_CHECK_IDLE;
				printf("WEIGHING_2 CHECK: Er\n");
			}	

			else
			{
				printf("WEIGHING_1 Read: Er\n");
				for (i = 0; i < len; i++)
					printf("%02x ",Weight_Read_Buf[i]);
				printf("\n");
			}	
		}	
	}	
	
	memset(Weight_Read_Buf,0, sizeof(Weight_Read_Buf));
	len = USART3_Read_NoBlock(Weight_Read_Buf , sizeof(Weight_Read_Buf));
	if (len > 0)
	{	
		if (Weight_Check_Value_0(Weight_Read_Buf,len,&weight) == 0)
		{
//			printf("%s\n",Weight_Read_Buf);
//			printf("WEIGHING_2: %d\n", weight);
			XRJ_mutex_lock(&Device_Value.Lock);
			Device_Value.WEIGHING_2_Report = weight*10;
			XRJ_mutex_unlock(&Device_Value.Lock);	
		}		
		else
		{
			for (i = 0; i < len; i++)
				printf("%02x ",Weight_Read_Buf[i]);
			printf("\n");
			printf("%s\n",Weight_Read_Buf);
			ret = Weight_Check_ACK(Weight_Read_Buf,len);
			
			if ((ret == 0) && (Weight_Set_Proc2 != D_WEIGHT_IDLE))
			{	
				printf("WEIGHING_2 set[%d]\n",Weight_Set_Proc2);
				Weight_Set_Proc2++; 	
				if (Weight_Set_Proc2 > D_WEIGHT_ZERO_ACK)
				{
					Weight_Set_Proc2 = D_WEIGHT_IDLE;
				}	
			}
			else if ((ret == 1) && (Weight_Set_Proc2 != D_WEIGHT_IDLE))
			{
				printf("WEIGHING_2 set WT\n");
			}
			else if ((ret == -1) && (Weight_Set_Proc2 != D_WEIGHT_IDLE))
			{
				Weight_Set_Proc2 = D_WEIGHT_IDLE;
				printf("WEIGHING_2 Set: Er\n");
			}
			
			
			
			else if ((ret == 0) && (Weight_Check_Proc2 != D_WEIGHT_CHECK_IDLE))
			{
				printf("WEIGHING_2 CHECK[%d]\n",Weight_Check_Proc2);
				Weight_Check_Proc2++; 
				if (Weight_Check_Proc2 > D_WEIGHT_CHECK_ACK)
				{
					Weight_Check_Proc2 = D_WEIGHT_CHECK_IDLE;
				}
				
			}	
			else if  ((ret == 1) && (Weight_Check_Proc2 != D_WEIGHT_CHECK_IDLE))
			{
				printf("WEIGHING_2 CHECK WT[%d]\n",Weight_Check_Proc2);
			}	
			else if  ((ret == -1) && (Weight_Check_Proc2 != D_WEIGHT_CHECK_IDLE))
			{
				Weight_Check_Proc2 = D_WEIGHT_CHECK_IDLE;
				printf("WEIGHING_2 CHECK: Er\n");
				for (i = 0; i < len; i++)
					printf("%02x ",Weight_Read_Buf[i]);
				printf("\n");
			}
			
			
			
			else
			{
				printf("WEIGHING_2 Read: Er\n");
				for (i = 0; i < len; i++)
					printf("%02x ",Weight_Read_Buf[i]);
				printf("\n");
			}	
		}	
	}
	
}	
#endif
